package com.tang.shopping.users.service.imp;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.tang.shopping.common.util.ConstatFinalUtil;
import com.tang.shopping.common.util.PageInfoUtil;
import com.tang.shopping.users.dao.IAAdminsDao;
import com.tang.shopping.users.dao.IARoleDao;
import com.tang.shopping.users.pojo.AAdmins;
import com.tang.shopping.users.pojo.ARole;
import com.tang.shopping.users.service.IUsersService;

@Service("usersService")
public class UsersServiceImpl implements IUsersService {

    @Resource
    private IAAdminsDao adminsDao;
    @Resource
    private IARoleDao roleDao;

    @Override
    public AAdmins findOneAdminsService(Map<String, Object> condMap) {
	return this.adminsDao.findOne(condMap);
    }

    @Override
    public JSONObject saveOneAdminsService(AAdmins admins) {
	JSONObject resultJSON = new JSONObject();
	/* 影响条数 */
	int res = this.adminsDao.save(admins);
	/*
	 * String str = null ; str.toString();
	 */
	if (res > 0) {
	    resultJSON.put("code", "0");
	    resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));

	    /* 存储data */
	    JSONObject dataJSON = new JSONObject();
	    dataJSON.put("id", admins.getId() + "");
	    dataJSON.put("effect", res + "");

	    resultJSON.put("data", dataJSON);
	} else {
	    resultJSON.put("code", "1");
	    resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
	}
	return resultJSON;
    }

    @Override
    public JSONObject updateOneAdminsService(AAdmins admins) {
	JSONObject resultJSON = new JSONObject();
	/* 影响条数 */
	int res = this.adminsDao.update(admins);
	/*
	 * String str = null ; str.toString();
	 */
	if (res > 0) {
	    resultJSON.put("code", "0");
	    resultJSON.put("info", "成功");

	    /* 存储data */
	    JSONObject dataJSON = new JSONObject();
	    dataJSON.put("id", admins.getId() + "");
	    dataJSON.put("effect", res + "");

	    resultJSON.put("data", dataJSON);
	} else {
	    resultJSON.put("code", "2");
	    resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
	}
	return resultJSON;
    }

    @Override
    public JSONObject deleteOneAdminsService(AAdmins admins) {
	JSONObject resultJSON = new JSONObject();
	/* 影响条数 */
	int res = this.adminsDao.delete(admins);
	/*
	 * String str = null ; str.toString();
	 */
	if (res > 0) {
	    resultJSON.put("code", "0");
	    resultJSON.put("info", "成功");

	    /* 存储data */
	    JSONObject dataJSON = new JSONObject();
	    dataJSON.put("id", admins.getId() + "");
	    dataJSON.put("effect", res + "");

	    resultJSON.put("data", dataJSON);
	} else {
	    resultJSON.put("code", "3");
	    resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
	}
	return resultJSON;
    }

    @Override
    public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
	/*
	 * 为关键字加上%,实现模糊匹配
	 */
	if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + "")) {
	    condMap.put("keyword", "%" + condMap.get("keyword") + "%");
	}

	if (pageInfoUtil != null) {
	    /* 分页 */
	    /* 查询总记录数 */
	    condMap.put("pageCount", "true");
	    List<AAdmins> roleList = this.adminsDao.findCondList(condMap);
	    /* 取总记录数 */
	    if (roleList.size() == 1) {
		pageInfoUtil.setTotalRecord(roleList.get(0).getId());
	    }

	    /* 查询数据 */
	    condMap.put("pageCount", "false");
	    /* limit ? , ? */
	    condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
	    condMap.put("pageSize", pageInfoUtil.getPageSize());
	    return this.adminsDao.findCondList(condMap);
	}
	/* 不分页查询数据 */
	return this.adminsDao.findCondList(condMap);
    }

    @Override
    public ARole findOneRoleService(Map<String, Object> condMap) {
	return this.roleDao.findOne(condMap);
    }

    @Override
    public JSONObject saveOneRoleService(ARole role) {
	JSONObject resultJSON = new JSONObject();
	/* 影响条数 */
	int res = this.roleDao.save(role);
	/*
	 * String str = null ; str.toString();
	 */
	if (res > 0) {
	    resultJSON.put("code", "0");
	    resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));

	    /* 存储data */
	    JSONObject dataJSON = new JSONObject();
	    dataJSON.put("id", role.getId() + "");
	    dataJSON.put("effect", res + "");

	    resultJSON.put("data", dataJSON);
	} else {
	    resultJSON.put("code", "1");
	    resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
	}
	return resultJSON;
    }

    @Override
    public JSONObject updateOneRoleService(ARole role) {
	JSONObject resultJSON = new JSONObject();
	/* 影响条数 */
	int res = this.roleDao.update(role);
	/*
	 * String str = null ; str.toString();
	 */
	if (res > 0) {
	    resultJSON.put("code", "0");
	    resultJSON.put("info", "成功");

	    /* 存储data */
	    JSONObject dataJSON = new JSONObject();
	    dataJSON.put("id", role.getId() + "");
	    dataJSON.put("effect", res + "");

	    resultJSON.put("data", dataJSON);
	} else {
	    resultJSON.put("code", "2");
	    resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
	}
	return resultJSON;
    }

    @Override
    public JSONObject deleteOneRoleService(ARole role) {
	JSONObject resultJSON = new JSONObject();
	/* 影响条数 */
	int res = this.roleDao.delete(role);
	/*
	 * String str = null ; str.toString();
	 */
	if (res > 0) {
	    resultJSON.put("code", "0");
	    resultJSON.put("info", "成功");

	    /* 存储data */
	    JSONObject dataJSON = new JSONObject();
	    dataJSON.put("id", role.getId() + "");
	    dataJSON.put("effect", res + "");

	    resultJSON.put("data", dataJSON);
	} else {
	    resultJSON.put("code", "3");
	    resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
	}
	return resultJSON;
    }

    @Override
    public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
	/* 为关键字加上%,实现模糊匹配 */
	if (!"".equalsIgnoreCase(condMap.get("keyword") + "")) {
	    condMap.put("keyword", "%" + condMap.get("keyword") + "%");
	}

	if (pageInfoUtil != null) {
	    /* 分页 */
	    /* 查询总记录数 */
	    condMap.put("pageCount", "true");
	    List<ARole> roleList = this.roleDao.findCondList(condMap);
	    /* 取总记录数 */
	    if (roleList.size() == 1) {
		pageInfoUtil.setTotalRecord(roleList.get(0).getId());
	    }

	    /* 查询数据 */
	    condMap.put("pageCount", "false");
	    /* limit ? , ? */
	    condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
	    condMap.put("pageSize", pageInfoUtil.getPageSize());
	    return this.roleDao.findCondList(condMap);
	}
	/* 不分页查询数据 */
	return this.roleDao.findCondList(condMap);
    }
}
